<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="xbsj-labels" content="Earth示例">
    </meta>
    <title>Pin-onclick</title>
    <!-- 0 引入js文件：XbsjEarth.js和vue.min.js -->
    <script src="../../XbsjEarth/XbsjEarth.js"></script>
    <script src="./scripts/vue.min.js"></script>
    <style>
        html,
        body {
            width: 100%;
            height: 100%;
            margin: 0px;
            padding: 0px;
        }

        .checkbox {
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div id="vueApp" style="width: 100%; height: 100%; background: grey; position: relative;">
        <earth-comp></earth-comp>
    </div>

    <script>
        // 1 创建Earth的vue组件
        var EarthComp = {
            template: `
                <div style="width: 100%; height: 100%">
                    <div ref="earthContainer" style="width: 100%; height: 100%">
                    </div>
                    <div style="position: absolute; left: 18px; top: 18px; color: white; background: rgba(0, 0, 0, 0.5); padding: 5px; border-radius: 5px;">
                    </div>
                    <div 
                        v-show="showInfo" 
                        style="position: absolute; width: 100px; min-height: 20px; color: white; background: rgba(0, 0, 0, 0.7); padding: 5px; border-radius: 5px;"
                        :style="{ left: (winPos[0] + 30) + 'px', bottom: winPos[3] + 'px' }"
                    >
                        <span>点击飞入大雁塔！</span>
                    </div>
                </div>
            `,
            // <span @click="positionEditing1 = !positionEditing1" class="checkbox"><input type="checkbox" v-model="positionEditing1">瓦片1的位置编辑</span><br/>
            data() {
                return {
                    _earth: undefined, // 注意：Earth和Cesium的相关变量放在vue中，必须使用下划线作为前缀！
                    showInfo: true,
                    winPos: [0, 0, 0, 0], // left top right bottom
                };
            },
            // 1.1 资源创建
            mounted() {
                // 1.1.1 创建地球
                var earth = new XE.Earth(this.$refs.earthContainer);
                earth.interaction.picking.enabled = true;

                // 1.1.2 场景配置
                earth.sceneTree.root = {
                    "expand": true,
                    "title": "预览场景",
                    "children": [{
                        "ref": 'tileset',
                        "czmObject": {
                            "name": '大雁塔',
                            "xbsjType": "Tileset",
                            "url": '../assets/dayanta/tileset.json',
                            "xbsjUseOriginTransform": false,
                            "skipLevelOfDetail": false
                        },
                    }, {
                        "ref": 'pin1',
                        "czmObject": {
                            "name": 'Pin1',
                            "xbsjType": "Pin",
                            "position": [1.9016974701882112, 0.5972325152147303, 425.8641913624607],
                            "near": 100,
                        },
                    }, {
                        "czmObject": {
                            "name": '默认影像',
                            "xbsjType": "Imagery",
                            "xbsjImageryProvider": XE.Obj.Imagery.defaultImageryProviderConfig,
                        },
                    }]
                };

                var tileset = earth.sceneTree.$refs.tileset.czmObject;
                var pin1 = earth.sceneTree.$refs.pin1.czmObject;
                pin1.onclick = () => {
                    console.log('即将飞入大雁塔！');
                    tileset.flyTo();
                };

                pin1.onmouseover = () => {
                    pin1.scale = 1.1;
                    this.showInfo = true;
                }
                pin1.onmouseout = () => {
                    pin1.scale = 1.0;
                    this.showInfo = false;
                }

                // 1.1.5 数据绑定
                XE.MVVM.track(this, 'winPos', pin1, 'winPos');

                this._earth = earth;

                // only for Debug
                window.earth = earth;
                window.tileset = tileset;
                window.pin1 = pin1;
            },
            // 1.2 资源销毁
            beforeDestroy() {
                // vue程序销毁时，需要清理相关资源
                this._earth = this._earth && this._earth.destroy();
            },
        }

        // 2 创建vue程序
        // XE.ready()用来加载Cesium.js等相关资源
        XE.ready().then(() => {
            var app = new Vue({
                el: '#vueApp',
                components: {
                    EarthComp,
                },
            });
        });
    </script>
</body>

</html>